import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.swing.JOptionPane;

public class stc {

    public static void main(String[] args) {
        Map<String, List<String>> classes = new HashMap<>();
        Map<String, Map<String, Integer>> groups = new HashMap<>(); // 小组及其评分
        Map<String, List<String>> students = new HashMap<>();

        while (true) {
            int option = showMenu();

            switch (option) {
                case 1:
                    addClass(classes);
                    break;
                case 2:
                    manageClass(classes);
                    break;
                case 3:
                    addGroup(groups);
                    break;
                case 4:
                    manageGroup(groups);
                    break;
                case 5:
                    addStudent(students);
                    break;
                case 6:
                    manageStudent(students);
                    break;
                case 7:
                    randomRollCall(classes, groups, students);
                    break;
                case 8:
                    gradeGroup(groups);
                    break;
                case 9:
                    recordAttendance(students);
                    break;
                case 10:
                    recordAnswer(students);
                    break;
                case 11:
                    exportScores(classes, groups, students);
                    break;
                case 12:
                    JOptionPane.showMessageDialog(null, "您选择了退出");
                    System.exit(0);
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "无效的选项，请重新选择");
            }
        }
    }

    private static int showMenu() {
        Object[] options = {
                "1. 添加班级",
                "2. 管理班级",
                "3. 添加小组",
                "4. 管理小组",
                "5. 添加学生",
                "6. 管理学生",
                "7. 随机点名",
                "8. 给小组评分",
                "9. 记录学生考勤",
                "10. 记录学生答题情况",
                "11. 导出成绩",
                "12. 退出"
        };

        String selectedOption = (String) JOptionPane.showInputDialog(
                null,
                "请选择一个选项:",
                "菜单",
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[0]
        );

        if (selectedOption == null) {
            return -1; // 用户取消了选择
        }

        for (int i = 0; i < options.length; i++) {
            if (options[i].equals(selectedOption)) {
                return i + 1; // 返回选项编号
            }
        }

        return -1; // 无效选项
    }

    private static void addClass(Map<String, List<String>> classes) {
        String className = JOptionPane.showInputDialog("请输入班级名称:");
        if (className != null && !className.isEmpty()) {
            classes.put(className, new ArrayList<>());
            JOptionPane.showMessageDialog(null, "班级 " + className + " 已添加");
        } else {
            JOptionPane.showMessageDialog(null, "班级名称不能为空");
        }
    }

    private static void manageClass(Map<String, List<String>> classes) {
        if (classes.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有班级可管理");
            return;
        }

        Object[] classOptions = classes.keySet().toArray();
        String selectedClass = (String) JOptionPane.showInputDialog(
                null,
                "请选择要管理的班级:",
                "管理班级",
                JOptionPane.QUESTION_MESSAGE,
                null,
                classOptions,
                classOptions[0]
        );

        if (selectedClass == null) {
            return;
        }

        int classOption = JOptionPane.showOptionDialog(
                null,
                "请选择操作:\n1. 删除班级\n2. 查看班级学生",
                "管理班级 - " + selectedClass,
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                new Object[]{"1. 删除班级", "2. 查看班级学生"},
                "1. 删除班级"
        );

        switch (classOption) {
            case 0: // 删除班级
                classes.remove(selectedClass);
                JOptionPane.showMessageDialog(null, "班级 " + selectedClass + " 已删除");
                break;
            case 1: // 查看班级学生
                List<String> students = classes.get(selectedClass);
                if (students.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "班级 " + selectedClass + " 没有学生");
                } else {
                    StringBuilder studentList = new StringBuilder("班级 " + selectedClass + " 的学生列表:\n");
                    for (String student : students) {
                        studentList.append(student).append("\n");
                    }
                    JOptionPane.showMessageDialog(null, studentList.toString());
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "无效的操作");
        }
    }

    private static void addGroup(Map<String, Map<String, Integer>> groups) {
        String groupName = JOptionPane.showInputDialog("请输入小组名称:");
        if (groupName != null && !groupName.isEmpty()) {
            groups.put(groupName, new HashMap<>());
            JOptionPane.showMessageDialog(null, "小组 " + groupName + " 已添加");
        } else {
            JOptionPane.showMessageDialog(null, "小组名称不能为空");
        }
    }

    private static void manageGroup(Map<String, Map<String, Integer>> groups) {
        if (groups.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有小组可管理");
            return;
        }

        Object[] groupOptions = groups.keySet().toArray();
        String selectedGroup = (String) JOptionPane.showInputDialog(
                null,
                "请选择要管理的小组:",
                "管理小组",
                JOptionPane.QUESTION_MESSAGE,
                null,
                groupOptions,
                groupOptions[0]
        );

        if (selectedGroup == null) {
            return;
        }

        int groupOption = JOptionPane.showOptionDialog(
                null,
                "请选择操作:\n1. 删除小组\n2. 查看小组成员及评分",
                "管理小组 - " + selectedGroup,
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                new Object[]{"1. 删除小组", "2. 查看小组成员及评分"},
                "1. 删除小组"
        );

        switch (groupOption) {
            case 0: // 删除小组
                groups.remove(selectedGroup);
                JOptionPane.showMessageDialog(null, "小组 " + selectedGroup + " 已删除");
                break;
            case 1: // 查看小组成员及评分
                Map<String, Integer> members = groups.get(selectedGroup);
                if (members.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "小组 " + selectedGroup + " 没有成员");
                } else {
                    StringBuilder memberList = new StringBuilder("小组 " + selectedGroup + " 的成员列表及评分:\n");
                    for (Map.Entry<String, Integer> entry : members.entrySet()) {
                        memberList.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
                    }
                    JOptionPane.showMessageDialog(null, memberList.toString());
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "无效的操作");
        }
    }

    private static void addStudent(Map<String, List<String>> students) {
        String studentName = JOptionPane.showInputDialog("请输入学生姓名:");
        if (studentName != null && !studentName.isEmpty()) {
            students.put(studentName, new ArrayList<>());
            JOptionPane.showMessageDialog(null, "学生 " + studentName + " 已添加");
        } else {
            JOptionPane.showMessageDialog(null, "学生姓名不能为空");
        }
    }

    private static void manageStudent(Map<String, List<String>> students) {
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有学生可管理");
            return;
        }

        Object[] studentOptions = students.keySet().toArray();
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要管理的学生:",
                "管理学生",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentOptions,
                studentOptions[0]
        );

        if (selectedStudent == null) {
            return;
        }

        int studentOption = JOptionPane.showOptionDialog(
                null,
                "请选择操作:\n1. 删除学生\n2. 查看学生信息",
                "管理学生 - " + selectedStudent,
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                new Object[]{"1. 删除学生", "2. 查看学生信息"},
                "1. 删除学生"
        );

        switch (studentOption) {
            case 0: // 删除学生
                students.remove(selectedStudent);
                JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 已删除");
                break;
            case 1: // 查看学生信息
                List<String> info = students.get(selectedStudent);
                if (info.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 没有更多信息");
                } else {
                    StringBuilder studentInfo = new StringBuilder("学生 " + selectedStudent + " 的信息:\n");
                    for (String detail : info) {
                        studentInfo.append(detail).append("\n");
                    }
                    JOptionPane.showMessageDialog(null, studentInfo.toString());
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "无效的操作");
        }
    }

    private static void randomRollCall(Map<String, List<String>> classes, Map<String, Map<String, Integer>> groups, Map<String, List<String>> students) {
        if (classes.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有班级可进行随机点名");
            return;
        }

        Object[] classOptions = classes.keySet().toArray();
        String selectedClass = (String) JOptionPane.showInputDialog(
                null,
                "请选择要进行随机点名的班级:",
                "随机点名",
                JOptionPane.QUESTION_MESSAGE,
                null,
                classOptions,
                classOptions[0]
        );

        if (selectedClass == null) {
            return;
        }

        List<String> classStudents = classes.get(selectedClass);
        if (classStudents.isEmpty()) {
            JOptionPane.showMessageDialog(null, "班级 " + selectedClass + " 没有学生");
            return;
        }

        Random random = new Random();
        String selectedStudent = classStudents.get(random.nextInt(classStudents.size()));
        JOptionPane.showMessageDialog(null, "随机点名结果: " + selectedStudent);
    }

    private static void gradeGroup(Map<String, Map<String, Integer>> groups) {
        if (groups.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有小组可评分");
            return;
        }

        Object[] groupOptions = groups.keySet().toArray();
        String selectedGroup = (String) JOptionPane.showInputDialog(
                null,
                "请选择要评分的小组:",
                "给小组评分",
                JOptionPane.QUESTION_MESSAGE,
                null,
                groupOptions,
                groupOptions[0]
        );

        if (selectedGroup == null) {
            return;
        }

        Map<String, Integer> members = groups.get(selectedGroup);
        if (members.isEmpty()) {
            JOptionPane.showMessageDialog(null, "小组 " + selectedGroup + " 没有成员");
            return;
        }

        Object[] memberOptions = members.keySet().toArray();
        String selectedMember = (String) JOptionPane.showInputDialog(
                null,
                "请选择要评分的成员:",
                "给小组成员评分 - " + selectedGroup,
                JOptionPane.QUESTION_MESSAGE,
                null,
                memberOptions,
                memberOptions[0]
        );

        if (selectedMember == null) {
            return;
        }

        String scoreStr = JOptionPane.showInputDialog("请输入评分 (1-10):");
        try {
            int score = Integer.parseInt(scoreStr);
            if (score >= 1 && score <= 10) {
                members.put(selectedMember, score);
                JOptionPane.showMessageDialog(null, "成员 " + selectedMember + " 的评分为: " + score);
            } else {
                JOptionPane.showMessageDialog(null, "评分必须在1到10之间");
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "请输入有效的数字");
        }
    }

    private static void recordAttendance(Map<String, List<String>> students) {
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有学生可记录考勤");
            return;
        }

        Object[] studentOptions = students.keySet().toArray();
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要记录考勤的学生:",
                "记录考勤",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentOptions,
                studentOptions[0]
        );

        if (selectedStudent == null) {
            return;
        }

        String attendance = JOptionPane.showInputDialog("请输入考勤状态 (例如: 出勤/缺勤):");
        if (attendance != null && !attendance.isEmpty()) {
            List<String> studentInfo = students.get(selectedStudent);
            studentInfo.add("考勤: " + attendance);
            JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 的考勤状态已记录: " + attendance);
        } else {
            JOptionPane.showMessageDialog(null, "考勤状态不能为空");
        }
    }

    private static void recordAnswer(Map<String, List<String>> students) {
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有学生可记录答题情况");
            return;
        }

        Object[] studentOptions = students.keySet().toArray();
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要记录答题情况的学生:",
                "记录答题情况",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentOptions,
                studentOptions[0]
        );

        if (selectedStudent == null) {
            return;
        }

        String answer = JOptionPane.showInputDialog("请输入答题情况 (例如: 正确/错误):");
        if (answer != null && !answer.isEmpty()) {
            List<String> studentInfo = students.get(selectedStudent);
            studentInfo.add("答题情况: " + answer);
            JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 的答题情况已记录: " + answer);
        } else {
            JOptionPane.showMessageDialog(null, "答题情况不能为空");
        }
    }

    private static void exportScores(Map<String, List<String>> classes, Map<String, Map<String, Integer>> groups, Map<String, List<String>> students) {
        if (classes.isEmpty() || groups.isEmpty() || students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有数据可导出");
            return;
        }

        StringBuilder scores = new StringBuilder("导出的成绩:\n");

        // 导出班级信息
        for (Map.Entry<String, List<String>> entry : classes.entrySet()) {
            scores.append("班级: ").append(entry.getKey()).append("\n");
            for (String student : entry.getValue()) {
                scores.append("  学生: ").append(student).append("\n");
            }
        }

        // 导出小组信息
        for (Map.Entry<String, Map<String, Integer>> entry : groups.entrySet()) {
            scores.append("小组: ").append(entry.getKey()).append("\n");
            for (Map.Entry<String, Integer> member : entry.getValue().entrySet()) {
                scores.append("  成员: ").append(member.getKey()).append(", 评分: ").append(member.getValue()).append("\n");
            }
        }

        // 导出学生信息
        for (Map.Entry<String, List<String>> entry : students.entrySet()) {
            scores.append("学生: ").append(entry.getKey()).append("\n");
            for (String info : entry.getValue()) {
                scores.append("  信息: ").append(info).append("\n");
            }
        }

        JOptionPane.showMessageDialog(null, scores.toString());
    }
}
